package net.yegong.util;

import java.io.File;

public class FileSystem {

	public static File search(String filename, File dir) {
		for (File file : dir.listFiles()) {
			if (file.isDirectory()) {
				File retVal = search(filename, file);
				if (null != retVal) {
					return retVal;
				}
			}
			else if (file.isFile()) {
				if (file.getName().equalsIgnoreCase(filename)) {
					return file;
				}
			}
		}
		return null;
	}

	public static void delete(File path) {
		if (!path.exists()) {
			return;
		}
		File[] children = path.listFiles();
		for (File c : children) {
			if (c.isDirectory()) {
				delete(c);
			}
			if (c.isFile()) {
				c.delete();
			}
		}
		delete(path);
	}

	public static void empty(File path) {
		if (!path.exists()) {
			return;
		}
		File[] children = path.listFiles();
		for (File c : children) {
			if (c.isDirectory()) {
				delete(c);
			}
			if (c.isFile()) {
				c.delete();
			}
		}
	}
}
